package com.ndp.fb.rdb.db.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.PublishResultRdbService;
import com.ndp.fb.rdb.db.BatchPstAssign;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.PublishBatchResult;
import com.ndp.fb.rdb.model.PublishResult;
import com.ndp.fb.util.ListUtil;

/**
 * Created by infi on 2015/1/31.
 */
@Service
public class PublishResultRdbServiceImpl extends DaoTemplate implements PublishResultRdbService {

    @Override
    public Long save(PublishResult pr) {
        return saveResultId(pr);
    }

    @Override
    public List<Long> batchSave(final List<PublishResult> list)
    {
        String sql = "INSERT INTO publish_result " +
                "(batch_id, account_id, account_email, adcampaign_count, adset_count, adgroup_count, adcreative_count) " +
                "VALUES (?, ?, ?,?, ?, ?, ?)";
        return batchSaveResultId(sql, new BatchPstAssign() {
            @Override
            public void setParam(PreparedStatement ps, Object o) throws SQLException {
                PublishResult pr = (PublishResult) o;
                ps.setObject(1, pr.getBatchId());
                ps.setObject(2, pr.getAccountId());
                ps.setObject(3, pr.getAccountEmail());
                ps.setObject(4, pr.getAdcampaignCount());
                ps.setObject(5, pr.getAdsetCount());
                ps.setObject(6, pr.getAdgroupCount());
                ps.setObject(7, pr.getAdcreativeCount());
            }
        }, list);
    }

    @Override
    public List<PublishBatchResult> searchPublishResultInfo(Long batchId) {
        String sql = "SELECT pb.id,pb.publish_batch_name,pb.publish_status,pb.all_account_count,pb.all_campaign_count,pb.all_adset_count,pb.all_adgroup_count,pb.all_adcreative_count,pr.publish_result_id,e.fb_account_id,e.name,pr.adcampaign_count,pr.adset_count,pr.adgroup_count,pr.adcreative_count " +
                "FROM publish_batch pb INNER JOIN publish_result pr ON pb.id = pr.batch_id " +
                "INNER JOIN entrust_account e ON SUBSTRING(pr.account_id, 5, LENGTH(pr.account_id)) = e.fb_account_id " +
                "WHERE pb.id = " + batchId;

        List list = (List) getJdbcTemplate().query(sql, DefaultRowMapper.autoRowMapper(PublishBatchResult.class));
        if (list != null && list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public void batchDel(List<Long> ids) {
        String sql = "delete from publish_result where 1=1 AND publish_result_id = ?";
        getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                long id = ids.get(i);
                ps.setObject(1, id);
            }

            @Override
            public int getBatchSize() {
                return ids.size();
            }
        });
    }

    @Override
    public void batchUpdatePublishResultCount(List<PublishResult> list)
    {
        String sql = "UPDATE publish_result set " +
                "adcampaign_count=?, adset_count=?, adgroup_count=?, adcreative_count=? " +
                "where publish_result_id=?";
        if(ListUtil.isNotEmpty(list))
        {
            for(PublishResult o : list)
            {
                Object[] param = new Object[]{o.getAdcampaignCount(), o.getAdsetCount(), o.getAdgroupCount(), o.getAdcreativeCount(), o.getPublishResultId()};
                getJdbcTemplate().update(sql, param);
            }
        }
    }

    @Override
    public PublishResult searchPublishResultCount(Long fbAccountId) {
        String sql = "SELECT t2.account_id,SUM(t2.adcampaign_count) AS adcampaign_count,SUM(t2.adset_count) AS adset_count,SUM(t2.adgroup_count) AS adgroup_count,SUM(t2.adcreative_count) AS adcreative_count FROM publish_result t2 " +
                "WHERE t2.account_id=?  AND t2.batch_id IN (SELECT t.id FROM publish_batch t WHERE t.publish_status = 1) GROUP BY t2.account_id";
        return findOne(sql, new Object[]{"act_" + fbAccountId}, DefaultRowMapper.autoRowMapper(PublishResult.class));
    }


}
